﻿/* ***** BEGIN LICENSE BLOCK *****
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * 
 * The Original Code is CnCNet Client code, Released 2nd January 2009.
 * 
 * The Initial Developers of the Original Code are
 * Adam Krock and John Sanderson. Portions created by
 * the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s): Adam Krock, John Sanderson.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either of the GNU General Public License Version 2 or later (the "GPL"),
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK *****/

using System;
using System.Xml;
using System.Xml.XPath;

namespace CnCNet
{
    public class cncNetXml
    {
        // File name and document
        private string myDocumentName = "cncNet.xml";
        private XmlDocument myDocument;
        private XmlElement myDocumentElement;

        public cncNetXml()
        {
            try
            {
                myDocument = new XmlDocument();
                myDocument.Load(myDocumentName);
                myDocumentElement = myDocument.DocumentElement;
            }
            catch (XmlException)
            {

            }
        }

        #region File Structure Manipulation Methods

        /// <summary>
        /// Added by John on 17/04/2009.
        /// Adds an element to the XML document and specifies a value.
        /// </summary>
        /// <param name="section">The parent element for the new element</param>
        /// <param name="param">The name of the new element</param>
        /// <param name="value">The contents of the new element</param>
        public void addXmlElement(string section, string param, string value)
        {
            XmlElement newElem = myDocument.CreateElement(param);
            myDocumentElement.SelectSingleNode("/cncNet/" + section).AppendChild(newElem);
            myDocumentElement.SelectSingleNode("/cncNet/" + section + "/" + param).InnerText = value;
        }

        /// <summary>
        /// Added by John on 17/04/2009.
        /// Adds an element to the XML document.
        /// </summary>
        /// <param name="section">The parent element for the new element</param>
        /// <param name="param">The name of the new element</param>
        public void addXmlElement(string section, string param)
        {
            addXmlElement(section, param, "");
        }

        /// <summary>
        /// Added by John on 17/04/2009.
        /// Checks if an element exists in the XML Document.
        /// </summary>
        /// <param name="section">The parent element for the element</param>
        /// <param name="param">The name of the element</param>
        public bool checkXmlElement(string section, string param)
        {
            if (myDocumentElement.SelectSingleNode("/cncNet/" + section + "/" + param) == null)
                return false;
            return true;
        }

        public bool checkXmlElement(string section)
        {
            if (myDocumentElement.GetElementsByTagName(section) == null)
                return false;
            return true;
        }

        #endregion

        #region User Settings

        public string CNCNET_USER_NAME
        {
            get
            {
                string tempOne = myDocumentElement.SelectSingleNode("/cncNet/user/name").InnerText;
                if (tempOne.Length < 12)
                {
                    return tempOne;
                }
                else
                {
                    return tempOne.Substring(0, 11);
                }
                
            }
            set
            {
                myDocumentElement.SelectSingleNode("/cncNet/user/name").InnerText = value;
                myDocument.Save(myDocumentName);
            }
        }

        public string CNCNET_USER_PASSWORD
        {
            get
            {
                string tempOne = myDocumentElement.SelectSingleNode("/cncNet/user/password").InnerText;
                if (tempOne.Length < 12)
                {
                    return tempOne;
                }
                else
                {
                    return tempOne.Substring(0, 11);
                }
            }
            set
            {
                myDocumentElement.SelectSingleNode("/cncNet/user/password").InnerText = value;
                myDocument.Save(myDocumentName);
            }
        }

        public ushort CNCNET_USER_PORT
        {
            get
            {
                return ushort.Parse(myDocumentElement.SelectSingleNode("/cncNet/user/port").InnerText);
            }
            set
            {
                myDocumentElement.SelectSingleNode("/cncNet/user/port").InnerText = value.ToString();
                myDocument.Save(myDocumentName);
            }
        }

        public int CNCNET_USER_BUFFERSIZE
        {
            get
            {
                return int.Parse(myDocumentElement.SelectSingleNode("/cncNet/user/buffersize").InnerText);
            }
            set
            {
                myDocumentElement.SelectSingleNode("/cncNet/user/buffersize").InnerText = value.ToString();
                myDocument.Save(myDocumentName);
            }
        }

        public string CNCNET_USER_EXTERNALIP
        {
            get
            {
                return myDocumentElement.SelectSingleNode("/cncNet/user/externalip").InnerText;
            }
            set
            {
                myDocumentElement.SelectSingleNode("/cncNet/user/externalip").InnerText = value;
                myDocument.Save(myDocumentName);
            }
        }

        public int CNCNET_USER_UPDATERATE
        {
            get
            {
                int tempOne = int.Parse(myDocumentElement.SelectSingleNode("/cncNet/user/updaterate").InnerText);
                if(tempOne < 500)
                {
                    return 500;
                }
                else
                {
                    return tempOne;
                }
            }
            set
            {
                myDocumentElement.SelectSingleNode("/cncNet/user/updaterate").InnerText = value.ToString();
                myDocument.Save(myDocumentName);
            }
        }

        public string CNCNET_USER_UPDATECLIENTSRC
        {
            get
            {
                return myDocumentElement.SelectSingleNode("/cncNet/user/updateclientsrc").InnerText;
            }
            set
            {
                myDocumentElement.SelectSingleNode("/cncNet/user/updateclientsrc").InnerText = value;
                myDocument.Save(myDocumentName);
            }
        }

        public string CNCNET_USER_UPDATECLIENTDST
        {
            get
            {
                return myDocumentElement.SelectSingleNode("/cncNet/user/updateclientdst").InnerText;
            }
            set
            {
                myDocumentElement.SelectSingleNode("/cncNet/user/updateclientdst").InnerText = value;
                myDocument.Save(myDocumentName);
            }
        }

        public string CNCNET_USER_UPDATECLIENTHOST
        {
            get
            {
                return myDocumentElement.SelectSingleNode("/cncNet/user/updateclienthost").InnerText;
            }
            set
            {
                myDocumentElement.SelectSingleNode("/cncNet/user/updateclienthost").InnerText = value;
                myDocument.Save(myDocumentName);
            }
        }

        public string CNCNET_USER_UPDATECLIENTINFO
        {
            get
            {
                return myDocumentElement.SelectSingleNode("/cncNet/user/updateclientinfo").InnerText;
            }
            set
            {
                myDocumentElement.SelectSingleNode("/cncNet/user/updateclientinfo").InnerText = value;
                myDocument.Save(myDocumentName);
            }
        }

        public string CNCNET_USER_UPDATECLIENTEXE
        {
            get
            {
                return myDocumentElement.SelectSingleNode("/cncNet/user/updateclientexe").InnerText;
            }
            set
            {
                myDocumentElement.SelectSingleNode("/cncNet/user/updateclientexe").InnerText = value;
                myDocument.Save(myDocumentName);
            }
        }

        public string CNCNET_USER_GAMEPATH
        {
            get
            {
                return myDocumentElement.SelectSingleNode("/cncNet/user/gamepath").InnerText;
            }
            set
            {
                myDocumentElement.SelectSingleNode("/cncNet/user/gamepath").InnerText = value;
                myDocument.Save(myDocumentName);
            }
        }

        public bool CNCNET_USER_DEBUG
        {
            get
            {
                return bool.Parse(myDocumentElement.SelectSingleNode("/cncNet/user/debug").InnerText);
            }
            set
            {
                myDocumentElement.SelectSingleNode("/cncNet/user/debug").InnerText = value.ToString();
                myDocument.Save(myDocumentName);
            }
        }

        public bool CNCNET_USER_GEOLOCATION
        {
            get
            {
                return bool.Parse(myDocumentElement.SelectSingleNode("/cncNet/user/geolocation").InnerText);
            }
            set
            {
                myDocumentElement.SelectSingleNode("/cncNet/user/geolocation").InnerText = value.ToString();
                myDocument.Save(myDocumentName);
            }
        }

        /// <summary>
        /// Added by John on 17/04/2009
        /// Contains a serialized representaion of the original settings in
        /// conquer.ini if they were changed by the program.
        /// </summary>
        public string CNCNET_USER_CONQUER
        {
            get
            {
                return this.myDocumentElement.SelectSingleNode("/cncNet/user/conquer").InnerText;
            }
            set
            {
                this.myDocumentElement.SelectSingleNode("/cncNet/user/conquer").InnerText = value;
                this.myDocument.Save(this.myDocumentName);
            }
        }

        /// <summary>
        /// Added by John on 17/04/2009
        /// Contains the version of the XML File
        /// </summary>
        public string CNCNET_USER_VERSION
        {
            get
            {
                return this.myDocumentElement.SelectSingleNode("/cncNet/user/version").InnerText;
            }
            set
            {
                this.myDocumentElement.SelectSingleNode("/cncNet/user/version").InnerText = value;
                this.myDocument.Save(this.myDocumentName);
            }
        }

        public bool CNCNET_USER_UPNP
        {
            get
            {
                return bool.Parse(this.myDocumentElement.SelectSingleNode("/cncNet/user/upnp").InnerText);
            }
            set
            {
                this.myDocumentElement.SelectSingleNode("/cncNet/user/upnp").InnerText = value.ToString();
                this.myDocument.Save(this.myDocumentName);
            }
        }

        #endregion

        #region Database settings

        public string CNCNET_DATABASE_HOST
        {
            get
            {
                return myDocumentElement.SelectSingleNode("/cncNet/database/host").InnerText;
            }
            set
            {
                myDocumentElement.SelectSingleNode("/cncNet/database/host").InnerText = value;
            }
        }

        #endregion

    }
}
